package com.wolfalpha.utron.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.wolfalpha.utron.R;
import com.wolfalpha.utron.activity.base.BaseNormalActivity;
import com.wolfalpha.utron.bean.PartnerInfo;
import com.wolfalpha.utron.service.PartnerService;
import com.wolfalpha.utron.service.impl.PartnerServiceImpl;
import com.wolfalpha.utron.utils.ThreadPoolUtils;

/**
 * 个人基本信息编辑界面
 * <p/>
 * Created by luodon on 15/8/21.
 */
public class PartnerInfoOtherActivity extends BaseNormalActivity {

    private TextView tvNothing;  //什么都没有
    private TextView tvNotAuth;  //未认证

    private ScrollView svDetail;    //详细信息

    private LinearLayout llAuthed;  //认证信息


    private TextView tvRole;    //昵称
    private TextView tvAbility;     //年龄
    private TextView tvWilling;  //学校
    private TextView tvTimein;    //邮箱
    private TextView tvExper;    //故乡
    private TextView tvDy;    //签名

    private TextView tvYx;    //签名
    private TextView tvNj;    //签名

    private int userId = 0;

    private PartnerInfo partnerInfo;  //合伙人信息

    private PartnerService partnerService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getIntent().getExtras();
        if (bundle == null || !bundle.containsKey("userId")) {
            finish();
        }
        userId = bundle.getInt("userId");

        setContentView(R.layout.activity_partner_view_other);
        partnerService = new PartnerServiceImpl();

        initView();
        loadData();
    }

    /**
     * 初始化界面
     */
    private void initView() {
        setTvTitle("查看合伙人");

        tvNothing = (TextView) findViewById(R.id.tv_partner_view_other_nothing);
        tvNotAuth = (TextView) findViewById(R.id.tv_partner_view_other_not_auth);

        svDetail = (ScrollView) findViewById(R.id.sv_partner_view_other_detail);
        llAuthed = (LinearLayout) findViewById(R.id.ll_partner_view_other_authed);

        tvRole = (TextView) findViewById(R.id.tv_partner_view_other_role);
        tvAbility = (TextView) findViewById(R.id.tv_partner_view_other_ability);
        tvWilling = (TextView) findViewById(R.id.tv_partner_view_other_willing);
        tvTimein = (TextView) findViewById(R.id.tv_partner_view_other_timein);
        tvExper = (TextView) findViewById(R.id.tv_partner_view_other_experience);
        tvDy = (TextView) findViewById(R.id.tv_partner_view_other_dy);

        tvYx = (TextView) findViewById(R.id.tv_partner_view_other_auth_yx);
        tvNj = (TextView) findViewById(R.id.tv_partner_view_other_auth_nj);

    }

    /**
     * 加载数据
     */
    private void loadData() {
        ThreadPoolUtils.execute(new Runnable() {
            @Override
            public void run() {
                partnerInfo = partnerService.getOtherInfo(userId);
                if (partnerInfo == null) handler.sendEmptyMessage(FAIL);
                else handler.sendEmptyMessage(SUCCESS);
            }
        });
    }


    private final int SUCCESS = 1;
    private final int FAIL = 2;


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SUCCESS:
                    int l = partnerInfo.getLevel();
                    if (l == 1) {
                        tvNothing.setVisibility(View.GONE);
                        svDetail.setVisibility(View.VISIBLE);
                        tvNotAuth.setVisibility(View.VISIBLE);
                        llAuthed.setVisibility(View.GONE);
                    } else if (l == 2) {
                        tvNothing.setVisibility(View.GONE);
                        tvNotAuth.setVisibility(View.GONE);
                        svDetail.setVisibility(View.VISIBLE);
                        llAuthed.setVisibility(View.VISIBLE);
                    }
                    if (l == 1 || l == 2) {
                        tvRole.setText(partnerInfo.getGood_partner().getRole());
                        tvAbility.setText(partnerInfo.getGood_partner().getAbility());
                        tvWilling.setText(partnerInfo.getGood_partner().getWilling());
                        tvTimein.setText(partnerInfo.getGood_partner().getTimein());
                        tvExper.setText(partnerInfo.getGood_partner().getExperience());
                        tvDy.setText(partnerInfo.getGood_partner().getDy());
                    }
                    if (l == 8) {
                        tvYx.setText(partnerInfo.getAuth_partner().getYx());
                        tvNj.setText(partnerInfo.getAuth_partner().getNj());
                    }
                    break;
                case FAIL:
                    svDetail.setVisibility(View.GONE);
                    llAuthed.setVisibility(View.GONE);
                    tvNothing.setVisibility(View.VISIBLE);
                    break;
            }
        }
    };


}
